<?php

namespace App\Service\OpenShop\SiteLocation;

use App\Enums\BaseErrorCode;
use App\Enums\OpenShop\InvestmentPromotion\MainProcessEnum;
use App\Enums\OpenShop\InvestmentPromotion\OrganizationManageEnum;
use App\Enums\OpenShop\InvestmentPromotion\SiteLocationEnum;
use App\Enums\OpenShop\InvestmentPromotion\UploadFileResourceEnum;
use App\Exceptions\Custom\ResponseHttpException;
use App\Models\OpenShop\InvestmentPromotion\OpenShopLocationModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopOrganizationManageModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUploadFileResourceModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationCustomerAnalysisModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationExamineLogModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationModel;
use App\Models\OpenShop\SiteLocation\OpenShopSiteLocationProductAnalysisModel;
use App\Service\OpenShop\Feishu\BaseService;
use App\Service\OpenShop\Feishu\MainProcessService;
use App\Service\OpenShop\Feishu\UploadFileResourceService;
use App\Support\AppletEncrypt;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Kuafu\File\FileUtil;
use Kuafu\IpWhite\Service\IpWhiteService;
use function GuzzleHttp\Promise\all;

class AuthService extends BaseService
{
    const TOKEN = 'site-token:';


    public static function getRmsUserInfo(Request $request)
    {

        if($request->bearerToken()){
            $userInfo = (array)$request->user($request->guard);
            $userInfo["userId"] = $userInfo["id"];
        }else{
            $token     = $request->header('APPLET-TOKEN');
            $userInfo  = AppletEncrypt::getInstance()->decryptData($token);
        }

        //获取身份信息
        $check = self::checkIsDistrictChief($userInfo['userId']);
        if ($check == SiteLocationEnum::POSITION_DEVELOP_MANAGER) {
            $userInfo['positionDesc'] = SiteLocationEnum::CN_POSITION_LIST[SiteLocationEnum::POSITION_DEVELOP_MANAGER];
        } elseif ($check == SiteLocationEnum::POSITION_PROVINCE_CHIEF) {
            $userInfo['positionDesc'] = SiteLocationEnum::CN_POSITION_LIST[SiteLocationEnum::POSITION_PROVINCE_CHIEF];
        } else {
            $userInfo['positionDesc'] = SiteLocationEnum::CN_POSITION_LIST[SiteLocationEnum::POSITION_HEAD];
        }
        $userInfo['position'] = $check;
        if ($userInfo['userId'] == config('duty.siteLocationDutyId')) {
            $userInfo['siteLocation'] = SiteLocationEnum::SITE_LOCATION_CHARGE_ON;
        } else {
            $userInfo['siteLocation'] = SiteLocationEnum::SITE_LOCATION_CHARGE_OFF;
        }

        return $userInfo;
    }


    public static function checkIsDistrictChief($userId)
    {
        $manage = OpenShopOrganizationManageModel::query()
                                                 ->where('operationUid', $userId)
                                                 ->first();
        if (empty($manage)) {
            return SiteLocationEnum::POSITION_DEVELOP_MANAGER;
        }
        if ($manage['type'] == OrganizationManageEnum::TYPE_HEADQUARTERS) {
            return SiteLocationEnum::POSITION_HEAD;
        }
        return SiteLocationEnum::POSITION_PROVINCE_CHIEF;
    }
}
